package com.wyth.study.advancedemo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NavigationActivity extends AppCompatActivity {


    @BindView(R.id.act_navigation_but_snackbar)
    Button mButSnackbar;
    @BindView(R.id.act_navigation_et_name)
    EditText mEtName;
    @BindView(R.id.act_navigation_textinput_name)
    TextInputLayout mTextinputName;
    @BindView(R.id.act_navigation_et_psd)
    EditText mEtPsd;
    @BindView(R.id.act_navigation_textinput_psd)
    TextInputLayout mTextinputPsd;
    @BindView(R.id.navigation_view)
    NavigationView mNavigationView;
    @BindView(R.id.act_navigation_drawerlayout)
    DrawerLayout mDrawerlayout;
    private boolean isFrist = true;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        ButterKnife.bind(this);
//        mEtName.setHint("name");
//        mEtPsd.setHint("password");
        mTextinputName.setHint("name");
        mTextinputPsd.setHint("password");
        initToolbar();
        initNavigation();
        initFloatActionButton();

    }

    private void initToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher_background);
//        actionBar.setDisplayHomeAsUpEnabled(true);
        // 显示应用的Logo
//        getSupportActionBar().setDisplayShowHomeEnabled(true);
//        getSupportActionBar().setDisplayUseLogoEnabled(true);
//        getSupportActionBar().setLogo(R.mipmap.ic_launcher);
        // 显示标题和子标题
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        toolbar.setTitle("Toolbar");
        toolbar.setSubtitle("the detail of toolbar");
        // 显示导航按钮
        toolbar.setNavigationIcon(R.drawable.ic_launcher_foreground);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initFloatActionButton() {
        final FloatingActionButton fab = ((FloatingActionButton) findViewById(R.id.act_navigation_float));
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "floatingActionBtn", Snackbar.LENGTH_SHORT)
                        .setAction("action", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Snackbar.make(fab, "???", 1).show();
                            }
                        }).show();
            }
        });
    }

    private void initNavigation() {
        if (mNavigationView != null) {
            mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Snackbar.make(mNavigationView, item.getTitle() + "dianji", Snackbar.LENGTH_SHORT).show();
                    item.setChecked(true);
                    mDrawerlayout.closeDrawers();
                    return true;
                }
            });
        }
    }


    @OnClick({R.id.act_navigation_but_snackbar, R.id.act_navigation_but_textInput})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.act_navigation_but_snackbar:
                Snackbar.make(mButSnackbar, "snackbar haha", Snackbar.LENGTH_SHORT)
                        .setAction("点一下", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(NavigationActivity.this, "点被", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();

                break;
            case R.id.act_navigation_but_textInput:
                isFrist = !isFrist;
                if (isFrist) {
                    mTextinputName.getEditText().setFocusable(true);
                    mTextinputName.getEditText().setFocusableInTouchMode(true);
                    mTextinputName.getEditText().requestFocus();
                    mTextinputName.setError("name error");
                    mTextinputPsd.setErrorEnabled(false);
                } else {
                    mTextinputPsd.getEditText().setFocusable(true);
                    mTextinputPsd.getEditText().setFocusableInTouchMode(true);
                    mTextinputPsd.getEditText().requestFocus();
                    mTextinputPsd.setError("psd error");
                    mTextinputName.setErrorEnabled(false);
                }
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.toolbar_action1:
                mDrawerlayout.openDrawer(Gravity.START);
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }
}
